//
//  ScrollTitleView.m
//  RedEnvelope
//
//  Created by iOS开发 on 2018/8/7.
//  Copyright © 2018年 iOS. All rights reserved.
//

#import "ScrollTitleView.h"
#import "ScrollTitleCell.h"
#import <Masonry.h>

@interface ScrollTitleView()
@property (nonatomic, assign) NSInteger showCount;
@property (nonatomic, assign) CGFloat animationContent;

@end

@implementation ScrollTitleView
{
    BOOL isStop;
}
- (void)dealloc {
    NSLog(@"滚动试图释放");
}

- (instancetype)initWithShowArr:(NSArray *)showArr ShowCount:(NSInteger)count scrollContent:(CGFloat)content {
    self = [super init];
    if (self) {
        _dataArr = [NSMutableArray arrayWithArray:showArr];
        _animationContent = content;
        _showCount = count;
    }
    return self;
}

// 开始动画
- (void)startAnimation {
    if (!_dataArr.count && _showCount != 0) {
        return;
    }
    isStop = NO;
    NSArray *viewArr;
    if (_dataArr.count > _showCount) {
        viewArr = [self animationToScrollTitle:[_dataArr subarrayWithRange:NSMakeRange(0, _showCount)]];
    } else {
        viewArr = [self animationToScrollTitle:_dataArr];
    }
    
    [UIView animateWithDuration:.4f animations:^{
        for (ScrollTitleCell *cell in viewArr) {
            cell.transform = CGAffineTransformIdentity;
        }
    } completion:^(BOOL finished) {
        // 进入递归
        if (self.dataArr.count > self.showCount) {
            [self recursiveAnimation:viewArr];
        }
    }];
}

// 递归动画
- (void)recursiveAnimation:(NSArray *)viewArr {
    if (isStop) {
        return;
    }
    // 循环数据
    [self forDataFromArr];
    // 获取下两条数据
    NSArray *otherViewArr = [self animationToScrollTitle:[_dataArr subarrayWithRange:NSMakeRange(0, _showCount)]];
    // 循环动画
    [UIView animateWithDuration:.4f delay:2 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        for (ScrollTitleCell *cell in viewArr) {
            cell.transform = CGAffineTransformMakeTranslation(0, -self.animationContent);
        }
        for (ScrollTitleCell *cell in otherViewArr) {
            cell.transform = CGAffineTransformIdentity;
        }
    } completion:^(BOOL finished) {
        for (ScrollTitleCell *cell in viewArr) {
            [cell removeFromSuperview];
        }
        [self recursiveAnimation:otherViewArr];
    }];
}


- (void)stopAnimation {
    isStop = YES;
}

// 循环数据
- (void)forDataFromArr {
    NSArray *tempArr = [_dataArr subarrayWithRange:NSMakeRange(0, _showCount)];
    [_dataArr removeObjectsInArray:tempArr];
    [_dataArr addObjectsFromArray:tempArr];
}


- (NSArray *)animationToScrollTitle:(NSArray *)titleCell {
    
    NSMutableArray *viewArr = [NSMutableArray array];
    for (ScrollTitleCell *cell in titleCell) {
        [self addSubview:cell];
        cell.transform = CGAffineTransformMakeTranslation(0, _animationContent);
        [viewArr addObject:cell];
    }
    
    if (viewArr.count > 1) {
        [viewArr mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:10 leadSpacing:10 tailSpacing:10];
        [viewArr mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).with.offset(0);
            make.right.mas_equalTo(self).with.offset(0);
        }];
    } else {
        [viewArr.firstObject mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).with.offset(0);
            make.right.mas_equalTo(self).with.offset(0);
            make.top.mas_equalTo(self).with.offset(10);
            make.height.mas_equalTo((self.frame.size.height - 20) / self.showCount);
        }];
    }
    
    
    return viewArr;
}

@end
